package com.hamizhong.iflowers.web.action.admin.statics;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.IPurposeService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.PurposeVO;

public class AjaxQueryPurposeAction extends MyBaseAjaxAction {

	private IPurposeService iPurposeService = null;
	
	public IPurposeService getiPurposeService() {
		return iPurposeService;
	}
	public void setiPurposeService(IPurposeService iPurposeService) {
		this.iPurposeService = iPurposeService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryPurposeAction...");
		String pageNum = getRQ().getParameter("pageNum");
		IOPResultDto<IObjectVO> resultDto = iPurposeService.findAll(Integer.valueOf(pageNum), MyConstants.PAGE_SIZE_COMMON_ADMIN);
		IObjectVO iObjectVO = resultDto.getFirstResult();
		List<PurposeVO> purposeVOList = iObjectVO.getPurposeVOList();
		int totalPurposeCnt = iObjectVO.getTotalPurposeCnt();
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		//设置总条数
		jo.put("totalCnt", totalPurposeCnt+"");
		//设置当前页
		jo.put("pageNum", pageNum);
		//设置总页数
		jo.put("totalPageCnt", MyTools.convertItemCnt2PageCnt(totalPurposeCnt, MyConstants.PAGE_SIZE_COMMON_ADMIN)+"");
		for (PurposeVO purposeVO : purposeVOList) {
			jobj = new JSONObject();
			jobj.put("purposeId", purposeVO.getPurposeId());
			jobj.put("purposeName", purposeVO.getPurposeName());
			jobj.put("sortId", purposeVO.getSortId());
			jobj.put("remarks", purposeVO.getRemarks());
			ja.add(jobj);
		}
		jo.put("data", ja.toString());
		jo.put("result", MyConstants.AJAX_SUCCESS);
		return jo;
	}

}
